var ajaxHelper = require("../../utils/ajaxhelp");
const serviceUrl = getApp().globalData.serviceUrl;
let token = wx.getStorageSync('token');

/**分页获取首页数据 */
const GetAppointments = (page, pageSize, starttime, endtime) => {
	token = wx.getStorageSync('token');
	return ajaxHelper.promiseRequst(serviceUrl + "/api/Appointment/GetAppointmentByPage", {
		'Authorization': 'Bearer ' + token
	}, "GET", {
		page: page,
		pageSize: pageSize,
		sort: 'desc',
		starttime: starttime,
		endtime: endtime
	})
}

/**根据日期获取数据 */
const GetAppointmentByDate = (startDate, endDate) => {
	token = wx.getStorageSync('token');
	return ajaxHelper.promiseRequst(serviceUrl + "/api/Appointment/GetAppointmentByTime", {
		'Authorization': 'Bearer ' + token
	}, "GET", {
		startDate: startDate,
		endDate: endDate
	})
}

/**根据id获取数据 */
const GetAppointmentById = (activityId) => {
	token = wx.getStorageSync('token');
	return ajaxHelper.promiseRequst(serviceUrl + "/api/Appointment/GetAppointmentById", {
		'Authorization': 'Bearer ' + token
	}, "GET", {
		activityId: activityId
	})
}

/**添加数据 */
const AddAppointment = (appointment = {}) => {
	token = wx.getStorageSync('token');
	return ajaxHelper.promiseRequst(serviceUrl + "/api/Appointment/AddAppointment", {
		'Authorization': 'Bearer ' + token
	}, "POST", appointment)
}

/**修改数据 */
const ModAppointment = (appointment = {}) => {
	token = wx.getStorageSync('token');
	return ajaxHelper.promiseRequst(serviceUrl + "/api/Appointment/ModAppointment", {
		'Authorization': 'Bearer ' + token
	}, "POST", appointment)
}

/**修改状态 */
const UpdateAppointmentState = (appointmentId, stateCode, statusCode) => {
	token = wx.getStorageSync('token');
	return ajaxHelper.promiseRequst(serviceUrl + "/api/Appointment/UpdateStatus?activityid=" + appointmentId +
		"&stateCode=" + stateCode + "&statusCode=" + statusCode, {
			'Authorization': 'Bearer ' + token,
			'content-type': 'application/x-www-form-urlencoded'
		}, "POST", {})
}

/**根据状态、时间分页获取数据 */
const GetAppointmentByState = (statecode, startDate, endDate, page, pageSize) => {
	token = wx.getStorageSync('token');
	return ajaxHelper.promiseRequst(serviceUrl + "/api/Appointment/GetAppointmentByState", {
		'Authorization': 'Bearer ' + token
	}, "GET", {
		statecode: statecode,
		startDate: startDate,
		endDate: endDate,
		page: page,
		pageSize: pageSize
	})
}

/**根据状态、时间、拜访对象分页获取数据 */
const GetAppointmentByContactId = (statecode, startDate, endDate, contactid, districtid, page, pageSize) => {
	token = wx.getStorageSync('token');
	return ajaxHelper.promiseRequst(serviceUrl + "/api/Appointment/GetAppointmentByContactId", {
		'Authorization': 'Bearer ' + token
	}, "GET", {
		page: page,
		pageSize: pageSize,
		statecode: statecode,
		contactid: contactid,
		starttime: startDate,
		endtime: endDate,
		districtid: districtid
	})
}

/**签到 */
const CheckIn = (appointment = {}) => {
	token = wx.getStorageSync('token');
	return ajaxHelper.promiseRequst(serviceUrl + "/api/Appointment/AppointmentCheckIn", {
		'Authorization': 'Bearer ' + token
	}, "POST", appointment)
}

/**完成 */
const CompeleteAppointment = (appointment = {}) => {
	token = wx.getStorageSync('token');
	return ajaxHelper.promiseRequst(serviceUrl + "/api/Appointment/AppointmentCompelete", {
		'Authorization': 'Bearer ' + token
	}, "POST", appointment)
}


//   /**获取当前地址 */
// const GetCurrentAddress=(location)=>{
//     return  ajaxHelper.promiseRequst("https://apis.map.qq.com/ws/geocoder/v1",
//     {  },"GET",{location:location,coord_type:1,key:'LHABZ-SMQCX-YSK4A-7PQPE-JTOGO-ZCFN2'})
//  }

/**获取当前地址 */
const GetCurrentAddress = (location) => {
	token = wx.getStorageSync('token');
	return ajaxHelper.promiseRequst("http://api.map.baidu.com/reverse_geocoding/v3/", {}, "GET", {
		output: 'json',
		coordtype: 'wgs84ll',
		location: location,
		ak: 'EEuZtEmCiQsIASGg3pOprRANoYdMOGtq'
	})
}



module.exports = {
	GetAppointments,
	GetAppointmentById,
	GetAppointmentByState,
	GetAppointmentByDate,
	AddAppointment,
	ModAppointment,
	UpdateAppointmentState,
	CheckIn,
	CompeleteAppointment,
	GetCurrentAddress,
	GetAppointmentByContactId,

}
